package Java.BlackHorse.a06ThreadTest.Test1;

import java.util.Random;

public class MyThread extends Thread{
    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    /*
    * 一共有三个红包，五个人去抢，最少抢到金额最少为0.01，总金额为100元
    * */
    static int count = 3;
    static double money = 100;
    static final double MIN = 0.01;
    @Override
    public void run() {
        synchronized(MyThread.class){
            if(count == 0){
                System.out.println(Thread.currentThread().getName()+"没有抢到红包");
            }else {
                if (count == 1) {
                    System.out.println(Thread.currentThread().getName() + "抢到了" + money + "元");
                    money = 0;
                    count = 0;
                } else {
                    Random r = new Random();
                    count--;
                    double price = r.nextDouble(money - MIN * count);
                    if (price < MIN) price = MIN;
                    money -= price;
                    System.out.println(Thread.currentThread().getName() + "抢到了" + price + "元");
                }
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
